|
|
|
|
|
|
|
you get a database error. It is best to avoid this error by limiting the creation of primary key indexes to CREATE TABLE statements. |
|
|
|
|
|
|
|
|
Using the UNIQUE KEY CONSTRAINT |
|
|
|
|
|
|
|
|
Another common use of the CONSTRAINT clause is in the creation of UNIQUE indexes. By default, the index key created using the UNIQUE CONSTRAINT clause allows null entries in the identified columns. However, when data is entered into the column, that data must be unique or the database engine returns an error message. This is the same as using the IGNORE NULL keyword in the CREATE INDEX statement. You should also note that you cannot use the DISALLOW NULL keywords when creating a UNIQUE CONSTRAINT clause. By default, all keys created using the UNIQUE CONSTRAINT are IGNORE NULL index keys. |
|
|
|
|
|
|
|
|
The SQL script in Listing 13.10 shows a new column in the JobsTable data table that was created in the last SQL-VB5 script. The new column, BudgetCode, is defined as an optional data column that must contain unique data. Update your version of the SQLVB04.SQV script, save it, and execute it. Your result set should resemble the one shown in Figure 13.13. |
|
|
|
|
|
|
|
|
Listing 13.10. Adding a UNIQUE CONSTRAINT. |
|
|
|
|
|
|
|
|
//
// sqlvb04.sqv - Test CONSTRAINT SQL keyword
//
// create a database
dbMake sqlvb04.mdb;
// create jobs title table
CREATE TABLE JobsTable
(JobTitle TEXT (20) CONSTRAINT PKJobTitle PRIMARY KEY,
BudgetCode TEXT(10) CONSTRAINT UKJobCode UNIQUE,
JobDesc MEMO
);
// show table
SELECT * FROM JobsTable;
// eof |
|
|
|
|
|
|
|
|
Figure 13.13.
Defining a UNIQUE CONSTRAINT index. |
|
|
|
|
|